package l3s.wikirevisions;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.Cookie;

import net.sourceforge.stripes.action.ActionBeanContext;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;

public class Utils {
	private static final String SOLR_SERVER = "http://localhost:8983/solr/";
	private static String sourceWikiPages ="WikiPoliticians";
	private static String sourceEvents = "events";
	
    private static SolrServer solrServerForWikiPages = null;
    private static SolrServer solrServerForEvents = null;
    private static Map<String, String> entities = null;
    private static Map<String, String> entities_to_id = null;
	
	public static SolrServer getSolrServerForWikiPages(){
		if (solrServerForWikiPages != null)
			return solrServerForWikiPages;
		
    	try{
    		solrServerForWikiPages = new HttpSolrServer(SOLR_SERVER + sourceWikiPages);    	
    	} catch (Exception e) {        	
			// TODO: handle exception
    		System.out.println("Exception here!!!");
    		e.printStackTrace();
		}
		
    	return solrServerForWikiPages;
	}
	
	public static SolrServer getSolrServerForWikiPages(String s){
		if (solrServerForWikiPages != null && s.equals(sourceWikiPages))
			return solrServerForWikiPages;
		
    	try{
    		sourceWikiPages = s;
    		solrServerForWikiPages = new HttpSolrServer(SOLR_SERVER + sourceWikiPages);    		
    	} catch (Exception e) {        	
			// TODO: handle exception
    		System.out.println("Exception here!!!");
    		e.printStackTrace();
		}
		
    	return solrServerForWikiPages;		
	}
	
    public static int getCookieLocation(String cookieName, ActionBeanContext context )
    {
    	Cookie[] cookies = context.getRequest().getCookies();
    	if( cookies != null )
	    	for( int i = 0; i < cookies.length; i++ )
	    		if( cookies[i].getName().trim().equals(cookieName.trim()))
	    			return i;
    	
    	//return negative value if not found 
    	return -1;	
    }

    public static String getSource(ActionBeanContext context){
        
    	String source = "WikiPoliticians";
    	
    	int sourceLoc = getCookieLocation("wikipages_source", context);
    	Cookie[] cookies = context.getRequest().getCookies();
    	System.out.println("Cookies: " + cookies);
    	/*
    	for (Cookie c: cookies){
    		System.out.println(c.getName() + "/" + c.getValue());
    	}
    	*/
    	if( sourceLoc != -1)
    		source = cookies[sourceLoc].getValue();    	
           
        return source;     
    }    		
	
	public static SolrServer getSolrServerForEvents(){
		if (solrServerForEvents != null)
			return solrServerForEvents;
		
    	try{
    		solrServerForEvents = new HttpSolrServer(SOLR_SERVER + sourceEvents);    	
    	} catch (Exception e) {        	
			// TODO: handle exception
    		System.out.println("Exception here!!!");
    		e.printStackTrace();
		}
		
    	return solrServerForEvents;
	}
	
		
	public static Map<String, String> getEntities(){
		if (entities != null)
			return entities;
		
		entities = new HashMap<String, String>();
		InputStreamReader ir = new InputStreamReader(Utils.class.getResourceAsStream("entities.txt"));
		BufferedReader br = new BufferedReader(ir);
		String line;
		try {
			while ((line = br.readLine()) != null){
				String[] splited = line.split("\\s");
				if (splited.length == 2 && !splited[1].equals("null")){
					entities.put(splited[1], splited[0]);
					//System.out.println(line);
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return entities;		
	}
	
	public static Map<String, String> getEntityToIDMap(){
		if (entities_to_id != null)
			return entities_to_id;
		
		entities_to_id = new HashMap<String, String>();
		InputStreamReader ir = new InputStreamReader(Utils.class.getResourceAsStream("entities.txt"));
		BufferedReader br = new BufferedReader(ir);
		String line;
		try {
			while ((line = br.readLine()) != null){
				String[] splited = line.split("\\s");
				if (splited.length == 2 && !splited[1].equals("null")){
					entities_to_id.put(splited[0], splited[1]);
					//System.out.println(line);
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return entities_to_id;		
	}
		
	public static void main(String args[]){
		getEntities();
		getEntityToIDMap();
		System.out.println(entities.size());
		List<String> abc = new ArrayList<String>();
		abc.add("a");
		abc.add("b");
		abc.add("c");
		abc.add("a");
		Set<String> entitiesv = new HashSet<String>(entities.values());
		System.out.println(entitiesv.size());
		System.out.println(entities_to_id.size());
		System.out.println(entities_to_id);
		//System.out.println(getContext().getSource());
	}
}
